from django.core.paginator import Paginator
from django.http import JsonResponse
from django.shortcuts import render
from .models import Collect, Course, Buy
from django.contrib.auth.views import login_required

num = 0

@login_required
# Create your views here.
def collect(request):
    # 找到当前用户的所有收藏记录
    collect = Collect.objects.filter(user=request.user)

    paginator = Paginator(collect, 2)
    page_num = request.GET.get("page")
    page = paginator.get_page(page_num)


    return render(request, "operation/collect.html", context=({
        "page": page
    }))


def change_collect(request):
    if request.method == "POST":
        course_id = request.POST.get("course_id")
        course = Course.objects.get(id=course_id)
        c = Collect.objects.filter(user=request.user, course=course).first()
        if c:
            course.collect_num -= 1
            course.save()
            c.delete()
            return JsonResponse({
                "code": 0,
                "msg": "取消收藏",
                "data": {
                    "state": True
                }
            })
        else:
            temp = Collect()
            temp.user = request.user
            temp.course = course
            temp.save()
            course.collect_num += 1
            course.save()
            return JsonResponse({
                "code": 0,
                "msg": "收藏成功",
                "data": {
                    "state": False
                }
            })
    else:
        return JsonResponse({
            "code": -1,
            "msg": "方法错误",
        })

@login_required
def buy_order(request):
    # 找到当前用户的所有收藏记录
    buy_order = Buy.objects.filter(user=request.user)
    buy_order1 = Buy.objects.filter(user=request.user,status=False)
    paginator1 = Paginator(buy_order1, 4)
    page_num1 = request.GET.get("page1")
    page1 = paginator1.get_page(page_num1)

    buy_order2 = Buy.objects.filter(user=request.user, status=True)
    paginator2 = Paginator(buy_order2, 4)
    page_num2 = request.GET.get("page2")
    page2 = paginator2.get_page(page_num2)
    return render(request, "operation/buy_order.html", context=({
        "buy_order": buy_order,
        "page1":page1,
        "page2": page2
    }))


def buy_order_change(request):
    if request.method == "POST":
        course_id = request.POST.get("course_id")
        course = Course.objects.get(id=course_id)
        c = Buy.objects.filter(user=request.user, course=course,status=False).first()
        if c:
            c.delete()
            return JsonResponse({
                "code": 0,
                "msg": "取消订单",
                "data": {
                    "state": True
                }
            })
        else:
            temp = Buy()
            temp.user = request.user
            temp.course = course
            temp.save()
            return JsonResponse({
                "code": 0,
                "msg": "添加订单",
                "data": {
                    "state": False
                }
            })
    else:
        return JsonResponse({
            "code": -1,
            "msg": "方法错误",
        })

def buy_order_status(request):
    if request.method == "POST":
        course_id = request.POST.get("course_id")
        course = Course.objects.get(id=course_id)
        c = Buy.objects.filter(user=request.user, course=course,status=False).first()
        if c:
            c.status = True
            c.save()
            return JsonResponse({
                "code": 0,
                "msg": "支付成功",
                "data": {
                    "state": True
                }
            })
        else:

            return JsonResponse({
                "code": 1,
                "msg": "支付失败",
                "data": {
                    "state": False
                }
            })
    else:
        return JsonResponse({
            "code": -1,
            "msg": "方法错误",
        })



def buy_again(request):
    if request.method == "POST":
        course_id = request.POST.get("course_id")
        course = Course.objects.get(id=course_id)
        again_order = Buy()
        again_order.user = request.user
        again_order.course = course
        again_order.status = False
        again_order.save()
        return JsonResponse({
            "code": 0,
            "msg": "再来一单",
            "data": {
                "status": True,
            }
        })


